import { Component, OnInit } from '@angular/core';
import { BladeContainerInterface } from "../../../../shared/blade/blade-container.interface";
import { Blade } from "../../../../shared/blade/blade.model";
import { Observable } from "rxjs/Observable";
import { FormBuilder, FormGroup, Validators, FormControl } from "@angular/forms";
import { SimpleChanges } from "@angular/core/core";
import { Command } from "../../../../shared/toolbar.service";
import { BladeContainerBase } from "../../../../shared/blade/blade-container-base";
import { BladeData } from "../../../../shared/blade/blade-data";
import { MyValidators } from "../../../../utils/validator/rang";
import { UserFarmAlarmconfig } from "../../../../entity/user_farm_alarmconfig.model";
import { UserFarmAlarmconfigService } from "../../../../service/user-farm-alarmconfig.service"

@Component({
  selector: 'app-userfarm-alarmconfig-edit',
  templateUrl: './userfarm-alarmconfig-edit.component.html',
  styleUrls: ['./userfarm-alarmconfig-edit.component.css']
})
export class UserfarmAlarmconfigEditComponent extends BladeContainerBase<UserFarmAlarmconfig> {
  private farmAlarms: UserFarmAlarmconfig[] = [];
  constructor(
    private fb: FormBuilder,
    private userFarmAlarmconfigService: UserFarmAlarmconfigService
  ) { super(fb) }

  ngOnInit() {
    this.farmAlarms = [...this.parentEntity];
    if (!this.isNew) {
      this.commands = [
        new Command("save", "保存", "fa fa-save", "setofkey:update", (blade) => this.exeSaveChange(blade), (blade) => this.canSaveChange(blade)),
        new Command("reset", "重置", "fa fa-save", "setofkey:reset", (blade) => this.exeReset(blade), (blade) => this.canReset(blade)),
      ]
    }
    super.ngOnInit();
  }

	/**
	 * 初始化数据
	 * 
	 * 
	 * @memberof TurbstatusValueListComponent
	 */
  initData(): any | Observable<any> | Promise<any> {
    this.formPatchValue(this.currentEntity);
  }

  exeSaveChange(blade: any): void {
    this.saveChange();
  }
  canSaveChange(blade: any): boolean {
    return this.hasChange() && !this.hasError();
  }
  exeReset(blade: any): void {
    return this.formGroup.reset(this.currentEntity);
  }
  canReset(blade: any): boolean {
    return this.hasChange();
  }

	/**
	  * 创建表单
	  * 
	  * 
	  * @memberof DicKeyEditComponent
	  */
  createForm() {
    this.formGroup = this.fromBuider.group({
      "farmName": this.currentEntity.farmName,
      "unAlarm": this.currentEntity.unAlarm
    });
  }
	/**
	* 设置表单错误对象
	* 
	* 
	* @memberof BladeContainerBase
	*/
  setFormError(): void {
    this.formErrors = {
      'farmName': '',
    };
  }
	/**
	* 设置表单验证信息
	* 
	* 
	* @memberof BladeContainerBase
	*/
  setValidationMessages(): void {
    this.validationMessages = {
      'farmName': {
        'required': '风场名称。',
      },
    };
  }

	/**
	 * 
	 * 
	 * 
	 * @memberof TurbstatusEditComponent
	 */
  saveChange(): any | Promise<any> | Observable<any> {
    if (this.canSaveChange(this.currentBlade)) {
      this.changeCurrent();
      // const index = this.farmAlarms.findIndex(o => o.farmId === this.currentEntity.farmId);
      // this.farmAlarms.splice(index, 1, this.currentEntity);
      return this.userFarmAlarmconfigService.update(this.currentEntity)
        .then(res => {
          if (res) {
            (this.parentBlade.bladeContainerInstance).refresh(res);
            this.bladeService.closeBlade(this.currentBlade);
            return true;
          }
        })
        .catch(error => this.bladeData.error = error)
        .then(res => this.bladeData.isLoading = false);
    }
  }
  changeCurrent() {
    this.bladeData.isLoading = true;
    const formValue = this.formGroup.value as UserFarmAlarmconfig;
    // this.currentEntity.alarmConfigId = (this.parentEntity as UserFarmAlarmconfig).alarmConfigId;
    // this.currentEntity.alarmCategoryId = (this.parentEntity as UserFarmAlarmconfig).alarmCategoryId;
    // this.currentEntity.farmId = (this.parentEntity as UserFarmAlarmconfig).farmId;
    // this.currentEntity.userId = (this.parentEntity as UserFarmAlarmconfig).userId;
    this.currentEntity.unAlarm = formValue.unAlarm;
  }

}



// WEBPACK FOOTER //
// ./src/app/module/basic/user-farm-alarmconfig/userfarm-alarmconfig-edit/userfarm-alarmconfig-edit.component.ts